package com.example.a22310.myapplication.Activity;
//俱乐部详情
//红色的R解决不了
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.a22310.myapplication.Beans.Club;
import com.example.a22310.myapplication.Fragment.Club.Item.ClubCourseFragment;
import com.example.a22310.myapplication.Fragment.Club.Item.ClubIntroFragment;
import com.example.a22310.myapplication.Fragment.Club.Item.ClubVideoFragment;
import com.example.a22310.myapplication.R;
import com.example.a22310.myapplication.ViewModel.Club.ClubDetailViewModel;
import com.example.a22310.myapplication.databinding.ActivityClubDetailBinding;
import com.squareup.picasso.Picasso;

public class ClubDetail extends AppCompatActivity implements View.OnClickListener
{
    private ActivityClubDetailBinding binding;
    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_club_detail);
        new ClubDetailViewModel(ClubDetail.this, binding);
        Intent intent=getIntent();
        Club.ClublistBean clublistBean=( Club.ClublistBean)intent.getSerializableExtra("club");
        binding.setIntro(clublistBean);
        String str =  clublistBean.getClubpic().substring(52,clublistBean.getClubpic().length());
        Picasso.with(this).load(str).into(binding.img);
        selectEvents();
        setSelect(0);

    }
    private void selectEvents(){
        binding.tabIntro.setOnClickListener(this);
        binding.tabCourse.setOnClickListener(this);
        binding.tabVideo.setOnClickListener(this);

    }
    private void setSelect(int i)
    {
        FragmentManager fm = ClubDetail.this.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {

            case 0:
                if (mTab01 == null) {
                    mTab01 = new ClubIntroFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }
                binding.intro.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
                binding.tabIntro.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.green));

                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new ClubCourseFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                binding.course.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
                binding.tabCourse.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.green));

                //tv1.setBackgroundColor(Color.BLUE);
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new ClubVideoFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else {
                    transaction.show(mTab03);
                }
                binding.video.setTextColor(ClubDetail.this.getResources().getColor(R.color.white));
                binding.tabVideo.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.green));

                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
    }

    private void resetImgs()
    {
        binding.tabIntro.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.colorDeepBlue));
        binding.tabCourse.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.colorDeepBlue));
        binding.tabVideo.setBackgroundColor(ClubDetail.this.getResources().getColor(R.color.colorDeepBlue));


        binding.intro.setTextColor(ClubDetail.this.getResources().getColor(R.color.green));
        binding.course.setTextColor(ClubDetail.this.getResources().getColor(R.color.green));
        binding.video.setTextColor(ClubDetail.this.getResources().getColor(R.color.green));
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_intro:
                setSelect(0);
                break;
            case R.id.tab_course:
                setSelect(1);
                break;
            case R.id.tab_video:
                setSelect(2);
                break;
            default:
                break;
        }

    }

}
